/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.google.code.netbuilder.dal.xmlaccess;

import com.google.code.netbuilder.dal.entities.*;
import java.io.File;
import java.util.ArrayList;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

/**
 *
 * @author Андрей
 */
public class XMLAccessor 
{
    private JAXBContext jaxbContext;
    private Marshaller marshaller;
    private Unmarshaller unmarshaller;
    private IAccessList processedList;
    
    private void defineContext(ArrayList<?> from, Class cl) throws Exception
    {
        if(cl.equals(Cable.class))
        {
          processedList = new CableAccessList((ArrayList<Cable>) from);   
          jaxbContext = JAXBContext.newInstance(CableAccessList.class);
        }
        else if(cl.equals(CableWire.class))
        {
          processedList = new CableWireAccessList((ArrayList<CableWire>) from);
          jaxbContext = JAXBContext.newInstance(CableWireAccessList.class);
        }
        else if(cl.equals(Equipment.class))
        {
          processedList = new EquipmentAccessList((ArrayList<Equipment>) from);
          jaxbContext = JAXBContext.newInstance(EquipmentAccessList.class);
        }
        else if(cl.equals(EquipmentPort.class))
        {
          processedList = new EquipmentPortAccessList((ArrayList<EquipmentPort>) from);
          jaxbContext = JAXBContext.newInstance(EquipmentPortAccessList.class);
        }
        else if(cl.equals(EquipmentVersion.class))
        {
          processedList = new EquipmentVersionAccessList((ArrayList<EquipmentVersion>) from);
          jaxbContext = JAXBContext.newInstance(EquipmentVersionAccessList.class);
        }
        else if(cl.equals(Port.class))
        {
          processedList = new PortAccessList((ArrayList<Port>) from); 
          jaxbContext = JAXBContext.newInstance(PortAccessList.class);
        }
        else if(cl.equals(Wire.class))
        {
          processedList = new WireAccessList((ArrayList<Wire>) from); 
          jaxbContext = JAXBContext.newInstance(WireAccessList.class);
        }
        else if(cl.equals(WireVersion.class))
        {
          processedList = new WireVersionAccessList((ArrayList<WireVersion>) from);
          jaxbContext = JAXBContext.newInstance(WireVersionAccessList.class);
        }
        else
        {
            throw new Exception("Unexpected type");
        }
        
        
        marshaller = jaxbContext.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        unmarshaller = jaxbContext.createUnmarshaller();
    }
    public void marshal(ArrayList<?> from, Class cl, String path) throws Exception
    {
        defineContext(from, cl);
        File outputFile = new File(path);
        marshaller.marshal(processedList, outputFile);    
    }
    
    public ArrayList<?> unmarshal(Class cl, String path) throws Exception
    {
        defineContext(null, cl);
        File inputFile = new File(path);
        processedList = (IAccessList)unmarshaller.unmarshal(inputFile);
        return processedList.getList();
    }
}
